using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Rolodex.Model;

namespace Rolodex.Views
{
    public partial class CardView : Form
    {
        #region Members
        private CardAdministrationMode _Mode;
        #endregion

        #region Properties
        public CardAdministrationMode Mode
        {
            get { return this._Mode; }
            set 
            { 
                this._Mode = value;
                this.FlexForm();
            }
        }

        public BindingSource Card
        {
            get { return this._Card; }
        }

        public TextBox textboxTitle
        {
            get { return this._textboxTitle; }
        }

        public TextBox textboxLetter
        {
            get { return this._textboxLetter; }
        }

        public Button buttonOK
        {
            get { return this._buttonOK; }
        }

        public Button buttonCancel
        {
            get { return this._buttonCancel; }
        }
        #endregion

        #region Methods
        public CardView()
        {
            InitializeComponent();
        }
        
        private void FlexForm()
        {
            switch (this._Mode)
            {
                case CardAdministrationMode.Add:
                    this.Text = "Add Card";
                    break;
                default:
                    this.Text = "Edit Card";
                    break;
            }
        }

        public bool IsFormValid()
        {
            this._ErrorProvider.Clear();

            bool IsFormValid =
                this.IsFieldValid(this._textboxTitle) &&
                this.IsFieldValid(this._textboxLetter);

            if (!IsFormValid)
            {
                MessageBox.Show(
                    "Not all required fields have been completed",
                    this.Text,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning,
                    MessageBoxDefaultButton.Button1);
            }

            return IsFormValid;
        }

        public bool IsFieldValid(Control theField)
        {
            if (null == theField)
            {
                return false;
            } 
            if (String.IsNullOrEmpty(theField.Text))
            {
                this._ErrorProvider.SetError(theField, "Required");
                return false;
            }
            return true;
        }
        #endregion
    }
}